﻿#include "hy_dashboard.h"

#include <math.h>
#include <QPainter>
#include <QDebug>


//NAMESPACE_BEGIN

HyDashboard::HyDashboard(QObject *parent)
    : QObject(parent)
{
	initParam();
}

HyDashboard::~HyDashboard()
{
}

bool HyDashboard::Attach(QWidget* widget)
{
	if (widget!=nullptr)
    {
        widget_ = widget;
        center_x_=widget_->width()>>1;
        center_y_=widget_->height()>>1;
        dial_side_ = qMin(widget_->width(), widget_->height());
		return true;
	}
	return false;
}

void HyDashboard::DrawDial(QPainter *painter)
{
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->translate(center_x_, center_y_);
    painter->scale(dial_side_ / 200.0, dial_side_ / 200.0);

	drawCrown(painter);
	drawScale(painter);
	drawScaleNum(painter);
    drawTitle(painter);
    drawNumericValue(painter);
	drawIndicator(painter);
}

void HyDashboard::SetDialRange(int min_value, int max_value)
{
	min_value_ = min_value;
	max_value_ = max_value;
}

void HyDashboard::drawCrown(QPainter *painter)
{
//    if(StyleType::STYLE_INTERNET==current_style_)
//    {
//        dial_backgroud_brush_=QColor(80, 80, 80);
//        dial_brush_=QColor(49, 50, 55);
//    }

	QRectF rect(-dial_radius_, -dial_radius_, dial_radius_ << 1, dial_radius_ << 1);

    //绘制背景圆
    painter->setPen(Qt::black);
    painter->setBrush(dial_backgroud_brush_);
	painter->drawEllipse(rect.adjusted(-dial_edge_radius_, -dial_edge_radius_, dial_edge_radius_, dial_edge_radius_));

	//绘制表盘
    //int arc_radius = dial_radius_ - dial_center_radius_;
    //QPainterPath path;
    //path.addEllipse(rect);
    //painter->drawPath(path);
    painter->setBrush(dial_brush_);
    painter->drawEllipse(rect);
#if 0
    /// 是否绘制中心圆
    if(StyleType::STYLE_INTERNET==current_style_)
    {
        int temp1=53;
        QRectF rect_color(-temp1, -temp1, temp1 << 1, temp1 << 1);
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::red);
        painter->drawPie(rect_color,-45*16,40*16);

        painter->setBrush(Qt::yellow);
        painter->drawPie(rect_color,-5*16,42*16);

        painter->setBrush(Qt::darkGreen);
        painter->drawPie(rect_color,37*16,188*16);

        painter->setBrush(Qt::gray);
        painter->drawEllipse(rect_color.adjusted(10, 10, -10, -10));

        QRectF rect1(-dial_center_radius_, -dial_center_radius_, dial_center_radius_ << 1, dial_center_radius_ << 1);
        painter->setPen(Qt::red);
        painter->setBrush(Qt::white);
        painter->drawEllipse(rect1);
    }
#endif
}

void HyDashboard::drawScale(QPainter *painter)
{
	//绘制刻度
	painter->save();
	painter->rotate(start_angle_);
	int steps = (scale_major_count_ * scale_minor_count_); //相乘后的值是分的份数 
	double angle_step = (360.0 - start_angle_ - end_angle_) / steps; //每一个份数的角度

	QPen pen;
	pen.setColor(scale_color_);
	for (int i = 0; i <= steps; i++)
	{
		if (i % scale_minor_count_ == 0)//整数刻度显示加粗 
		{
			pen.setWidth(1); //设置线宽 
			painter->setPen(pen); //使用面向对象的思想，把画笔关联上画家。通过画家画出来

            painter->drawLine(0, dial_radius_ - 35, 0, dial_radius_ - 25); //两个参数应该是两个坐标值
		}
		else
		{
			pen.setWidth(0);
			painter->setPen(pen);
            painter->drawLine(0, dial_radius_ - 35, 0, dial_radius_ - 30);
		}
		painter->rotate(angle_step);
	}
	painter->restore();
}

void HyDashboard::drawScaleNum(QPainter *painter)
{
	//绘制刻度值
	painter->save();
    painter->setPen(scale_number_color_);
	double startRad = (270 - start_angle_) * (3.14 / 180);
	double deltaRad = (360 - start_angle_ - end_angle_) * (3.14 / 180) / scale_major_count_;
	double sina, cosa;
	int x, y;

    QFont ft=painter->font();
    ft.setPixelSize(scale_number_font_size_);
    painter->setFont(ft);

    QFontMetricsF fm(ft);
	double w, h, tmpVal;
	QString str;

	for (int i = 0; i <= scale_major_count_; i++)
	{
        sina = sin(startRad - i * deltaRad);
        cosa = cos(startRad - i * deltaRad);

		tmpVal = 1.0 * i *((max_value_ - min_value_) / scale_major_count_) + min_value_;
		// tmpVal = 50;
		str = QString("%1").arg(tmpVal);  //%1作为占位符   arg()函数比起 sprintf()来是类型安全的
		w = fm.size(Qt::TextSingleLine, str).width();
		h = fm.size(Qt::TextSingleLine, str).height();
        x = (dial_radius_ - 15) * cosa - w / 2;
        y = -(dial_radius_ - 15) * sina + h / 4;
		painter->drawText(x, y, str); //函数的前两个参数是显示的坐标位置，后一个是显示的内容，是字符类型""

	}
	painter->restore();
}

void HyDashboard::drawNumericValue(QPainter *painter)
{
    /// 是否绘制中心圆
    if(StyleType::STYLE_INTERNET==current_style_)
    {
        /// 绘制三色环
        int temp1=53;
        QRectF rect_color(-temp1, -temp1, temp1 << 1, temp1 << 1);
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::red);
        painter->drawPie(rect_color,-45*16,270*16);
        painter->setBrush(Qt::green);
        painter->drawPie(rect_color,qRound(90.0/32.0*13)*16,qRound(90.0/32.0*38)*16);
//        painter->setBrush(Qt::darkGreen);
//        painter->drawPie(rect_color,37*16,188*16);
        painter->setBrush(Qt::darkGray);
        painter->drawEllipse(rect_color.adjusted(10, 10, -10, -10));
        ///绘制中心圆环
        painter->save();
        painter->setPen(Qt::red);  //没有线，填满没有边界
        painter->setBrush(Qt::darkGray);
        painter->drawEllipse(-6, -6, 12, 12);
        painter->restore();
        ///绘制指针
//        painter->save();
//        QPolygon pts;
//        pts.setPoints(4, -4,-10, 0, -12, 4, -10, 0, dial_radius_ - 30);
//        painter->rotate(start_angle_);
//        double degRotate = (360.0 - start_angle_ - end_angle_) / (max_value_ - min_value_)*(current_double_value_ - min_value_);
        painter->save();
        QPolygon pts;
        //pts.setPoints(3, -4, -15, 4, -15, 0, dial_radius_ - 35);	/* (-2,0)/(2,0)/(0,60) *///第一个参数是 ，坐标的个数。后边的是坐标
        pts.setPoints(4, -4,-10, 0, -12, 4, -10, 0, dial_radius_ - 35);
        painter->rotate(start_angle_);
        double tmp=current_double_value_ ;
        if(current_double_value_ - min_value_<0)tmp=min_value_;
        else if(current_double_value_ - max_value_>0)tmp=max_value_;
        double degRotate = (360.0 - start_angle_ - end_angle_) / (max_value_ - min_value_)*(tmp- min_value_);
        ///画指针
//        painter->rotate(degRotate);  //顺时针旋转坐标系统
//        painter->setPen(Qt::NoPen); //定义线条文本颜色  设置线条的颜色
//        painter->setBrush(needle_color_);//刷子定义形状如何填满 填充后的颜色
//        painter->drawConvexPolygon(pts); //这是个重载函数，绘制多边形。
//        painter->restore();
        painter->rotate(degRotate);  //顺时针旋转坐标系统
        painter->setPen(Qt::NoPen); //定义线条文本颜色  设置线条的颜色
        painter->setBrush(needle_color_);//刷子定义形状如何填满 填充后的颜色
        painter->drawConvexPolygon(pts); //这是个重载函数，绘制多边形。
        painter->restore();
        ///绘制中心圆环
//        QRectF rect1(-dial_center_radius_, -dial_center_radius_, dial_center_radius_ << 1, dial_center_radius_ << 1);
//        painter->setPen(Qt::red);
//        painter->setBrush(Qt::white);
//        painter->drawEllipse(rect1);


        drawTitle(painter);
        drawIndicator(painter);
    }
    else
    {
        ///画中心点
        painter->save();
        painter->setPen(Qt::red);  //没有线，填满没有边界
        painter->setBrush(Qt::darkGray);
        painter->drawEllipse(-6, -6, 12, 12);
        painter->restore();

        ///绘制指针
        painter->save();
        QPolygon pts;
        //pts.setPoints(3, -4, -15, 4, -15, 0, dial_radius_ - 35);	/* (-2,0)/(2,0)/(0,60) *///第一个参数是 ，坐标的个数。后边的是坐标
        pts.setPoints(4, -4,-10, 0, -12, 4, -10, 0, dial_radius_ - 35);
        painter->rotate(start_angle_);
        double degRotate = (360.0 - start_angle_ - end_angle_) / (max_value_ - min_value_)*(current_double_value_ - min_value_);
        ///画指针
        painter->rotate(degRotate);  //顺时针旋转坐标系统
        painter->setPen(Qt::NoPen); //定义线条文本颜色  设置线条的颜色
        painter->setBrush(needle_color_);//刷子定义形状如何填满 填充后的颜色
        painter->drawConvexPolygon(pts); //这是个重载函数，绘制多边形。
        painter->restore();
    }
}

void HyDashboard::drawTitle(QPainter *painter)
{
	painter->save();
	painter->setPen(text_color_);

    QFont ft=painter->font();
    ft.setPixelSize(scale_number_font_size_);
    painter->setFont(ft);
    QFontMetricsF fm2(ft);
	double w = fm2.size(Qt::TextSingleLine, dial_title_).width();
	painter->drawText(-w / 2, -20, dial_title_);
	painter->restore();
}

void HyDashboard::drawIndicator(QPainter *painter)
{
    QString str("0");
	QFontMetricsF fm1(widget_->font());
    double w(0.0);
    if(StyleType::STYLE_INTERNET==current_style_)
    {
//        str = QString::number(current_double_value_,'f',value_precision_).arg(scale_units_);
//        w = fm1.size(Qt::TextSingleLine, str).width();
//        painter->setPen(Qt::black);
//        painter->drawText(-w / 2, 5,str);
        str = QString(" %1 %2 ").arg(QString::number(current_double_value_,'f',value_precision_)).arg(scale_units_);
        w = fm1.size(Qt::TextSingleLine, str).width();
        painter->setPen(text_color_);
        painter->drawText(-w / 2, 60, str);
    }
    else if(StyleType::STYLE_DEFAULT==current_style_) {
        str = QString(" %1 %2 ").arg(QString::number(current_double_value_,'f',value_precision_)).arg(scale_units_);
        w = fm1.size(Qt::TextSingleLine, str).width();
        painter->setPen(text_color_);
        painter->drawText(-w / 2, 60, str);
    }
}

void HyDashboard::initParam()
{
    current_style_=StyleType::STYLE_DEFAULT;
    dial_radius_ = 90;
    dial_center_radius_ = 12;
    dial_edge_radius_ = 10;
	scale_minor_count_ = 10;
	scale_major_count_ = 10;
	start_angle_ = 45;
	end_angle_ = 45;

    max_value_ = 10000;//100;
    min_value_ = 0;
    current_double_value_ =0;

    center_x_=100;
    center_y_=100;
    dial_side_=200;
    scale_number_font_size_=16;

	dial_title_ = "Right brake";
    scale_units_ = "kg";

    dial_backgroud_brush_ = QColor(180, 180, 180);
	QRadialGradient gradient(0, 0, dial_radius_);
    //gradient.setColorAt(0, Qt::gray);
    //gradient.setColorAt(1.0, Qt::blue);
    gradient.setColorAt(0, Qt::blue);
    gradient.setColorAt(1.0, Qt::darkBlue);
    //dial_brush_ = gradient;
    dial_brush_=QColor(21, 16, 77);
    scale_color_ = QColor(180, 180, 180);
    //scale_number_color_ = QColor(0, 200, 0);
    scale_number_color_ = QColor(180, 180, 180);
    text_color_ = QColor(255, 255, 255);
    needle_color_=QColor(220,50,50);
	//qsrand(time(NULL));

	//connect(&timer_, SIGNAL(timeout()), SLOT(updateValue()));
	//timer_.start(100);
}

//NAMESPACE_END
